﻿using System;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class TronTrail : MonoBehaviour
{
    private bool m_AlwaysUp;
    private Color m_EndColor = new Color(1f, 1f, 1f, 0f);
    private float m_Height = 2f;
    private float m_MinDistance = 0.1f;
    private Color m_StartColor = Color.white;
    private float m_Time = 2f;
    private List<TrailSection> m_TrailSectionList;

    private void AddNewSection(Vector3 pos, float now)
    {
        if (this.m_TrailSectionList.Count != 0)
        {
            Vector3 vector = this.m_TrailSectionList[0].Positioin - pos;
            if (vector.sqrMagnitude <= (this.m_MinDistance * this.m_MinDistance))
            {
                return;
            }
        }
        TrailSection item = new TrailSection {
            Positioin = pos
        };
        if (this.m_AlwaysUp)
        {
            item.UpDir = Vector3.up;
        }
        else
        {
            item.UpDir = base.gameObject.transform.TransformDirection(Vector3.up);
        }
        item.Time = now;
        this.m_TrailSectionList.Insert(0, item);
    }

    private void Awake()
    {
        this.m_TrailSectionList = new List<TrailSection>();
    }

    public static GameObject Create(Material mat, Texture2D tex)
    {
        GameObject obj2 = new GameObject("Trail");
        obj2.AddComponent<TronTrail>();
        obj2.AddComponent<MeshFilter>();
        MeshRenderer renderer = obj2.AddComponent<MeshRenderer>();
        renderer.material = mat;
        renderer.material.mainTexture = tex;
        return obj2;
    }

    private void LateUpdate()
    {
        Vector3 position = base.gameObject.transform.position;
        float time = Time.time;
        this.RemoveOldSection(time);
        this.AddNewSection(position, time);
        this.RebuildMesh();
    }

    private void OnDisable()
    {
        this.m_TrailSectionList.Clear();
    }

    private void RebuildMesh()
    {
        Mesh mesh = base.GetComponent<MeshFilter>().mesh;
        mesh.Clear();
        int count = this.m_TrailSectionList.Count;
        if (count >= 2)
        {
            Vector3[] vectorArray = new Vector3[count * 2];
            Color[] colorArray = new Color[count * 2];
            Vector2[] vectorArray2 = new Vector2[count * 2];
            TrailSection section = this.m_TrailSectionList[0];
            Matrix4x4 worldToLocalMatrix = base.gameObject.transform.worldToLocalMatrix;
            for (int i = 0; i < count; i++)
            {
                section = this.m_TrailSectionList[i];
                float x = 0f;
                if (i != 0)
                {
                    x = Mathf.Clamp01((Time.time - section.Time) / this.m_Time);
                }
                Vector3 upDir = section.UpDir;
                vectorArray[i * 2] = worldToLocalMatrix.MultiplyPoint(section.Positioin);
                vectorArray[(i * 2) + 1] = worldToLocalMatrix.MultiplyPoint(section.Positioin + ((Vector3) (upDir * this.m_Height)));
                vectorArray2[i * 2] = new Vector2(x, 0f);
                vectorArray2[(i * 2) + 1] = new Vector2(x, 1f);
                Color color = Color.Lerp(this.m_StartColor, this.m_EndColor, x);
                colorArray[i * 2] = color;
                colorArray[(i * 2) + 1] = color;
            }
            int[] numArray = new int[((count - 1) * 2) * 3];
            for (int j = 0; j < (numArray.Length / 6); j++)
            {
                numArray[j * 6] = j * 2;
                numArray[(j * 6) + 1] = (j * 2) + 1;
                numArray[(j * 6) + 2] = (j * 2) + 2;
                numArray[(j * 6) + 3] = (j * 2) + 2;
                numArray[(j * 6) + 4] = (j * 2) + 1;
                numArray[(j * 6) + 5] = (j * 2) + 3;
            }
            mesh.vertices = vectorArray;
            mesh.colors = colorArray;
            mesh.uv = vectorArray2;
            mesh.triangles = numArray;
        }
    }

    private void RemoveOldSection(float now)
    {
        for (int i = this.m_TrailSectionList.Count; (i > 0) && (now > (this.m_TrailSectionList[i - 1].Time + this.m_Time)); i--)
        {
            this.m_TrailSectionList.RemoveAt(i - 1);
        }
    }

    public Color endColor
    {
        get
        {
            return this.m_EndColor;
        }
        set
        {
            this.m_EndColor = value;
        }
    }

    public float height
    {
        get
        {
            return this.m_Height;
        }
        set
        {
            this.m_Height = value;
        }
    }

    public Color startColor
    {
        get
        {
            return this.m_StartColor;
        }
        set
        {
            this.m_StartColor = value;
        }
    }

    private class TrailSection
    {
        private Vector3 m_Position = Vector3.zero;
        private float m_Time;
        private Vector3 m_UpDir = Vector3.zero;

        public Vector3 Positioin
        {
            get
            {
                return this.m_Position;
            }
            set
            {
                this.m_Position = value;
            }
        }

        public float Time
        {
            get
            {
                return this.m_Time;
            }
            set
            {
                this.m_Time = value;
            }
        }

        public Vector3 UpDir
        {
            get
            {
                return this.m_UpDir;
            }
            set
            {
                this.m_UpDir = value;
            }
        }
    }
}

